var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');

var app = getApp();
Page({
  data: {
    imgflag:true,
    saveflag:false,
    canIUseGetUserProfile: true,
    navUrl: '',
    code: '',
    avatarUrl:'https://www.rzjspxxh.com:9000/static/img/logo.jpg',
    theme: wx.getSystemInfoSync().theme,
    nickname:''
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    console.log("获取用户信息----登陆");
    let that = this;
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    if (wx.getStorageSync("navUrl")) {
      that.setData({
        navUrl: wx.getStorageSync("navUrl")
      })
    } else {
      that.setData({
        navUrl: '/pages/index/index'
      })
    }

    wx.login({
      success: function (res) {
        if (res.code) {
          that.setData({
            code: res.code
          })
        }
      }
    });

    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })

  },
  bindnicknameInput: function (e) {
    // console.log(e.detail.value);
    this.setData({
      nickname: e.detail.value
    });
  },
  onReady: function() {
    console.log("获取用户信息----登陆2");


  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  onChooseAvatar(e) {
    // console.log('eeee:::::'+JSON.stringify(e));
    const { avatarUrl } = e.detail 
    // console.log('eeee:::::'+JSON.stringify(avatarUrl));
    this.setData({
      avatarUrl,
      imgflag:false,
      saveflag:true,
    })
  },
  getUserProfile:function() {
    console.log("程序走到这里了！V2.1");
    var that = this;
    if(this.data.nickname == ""){
      wx.showModal({
        title: '昵称',
        content: '请填写昵称！',
        showCancel: false
      });
      return;
    }
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (resp) => {
        console.log("获取用户信息成功1：：：："+resp);
        console.log("获取用户信息成功2："+JSON.stringify(resp));
        console.log("获取用户信息成功3："+this.data.avatarUrl);
        console.log("获取用户信息成功4："+this.data.nickname);
        //console.log(JSON.stringify(resp.rawData));
        //console.log(JSON.stringify(resp.userInfo));
        //登录远程服务器
        this.loginByWeixin(resp);
        //console.log("程序走到这里了！V2.0");
      }
    })
  },
  loginByWeixin: function (e) {
    console.log("程序走到这里了！V3.0"+JSON.stringify(e.userInfo));
    console.log("程序走到这里了！V3.2"+JSON.stringify(e.rawData));
   // console.log(wx.getStorageSync ("shopInfo"));
    let that = this;
    if (e.userInfo == undefined) {
      if(e.rawData == undefined){
        app.globalData.hasLogin = false;
        wx.setStorageSync("app.globalData.hasLogin",false);
        util.showErrorToast('微信登录失败');
        return;
      }
    }
    
    var userinfo = e.userInfo;
    if(userinfo == ""){
      userinfo = e.rawData;
    }
    //console.log("VVVV3"+JSON.stringify(userinfo));

    //user.checkLogin().catch(() => {
      // console.log("22wwww");
      userinfo.nickName = this.data.nickname;
      userinfo.avatarUrl = this.data.avatarUrl;
      console.log("程序走到这里了！V3.3"+JSON.stringify(userinfo));
      user.loginByWeixin(userinfo).then(res => {
        console.log("微信授权登陆成功！");
        app.globalData.hasLogin = true;
        wx.setStorageSync("app.globalData.hasLogin",true);
        //登陆成功之后，返回首页
        wx.navigateBack({
          delta: 1
        })
      }).catch((err) => {
        app.globalData.hasLogin = false;
        wx.setStorageSync("app.globalData.hasLogin",false);
        util.showErrorToast('微信登录失败');
      });
    //});



  },
  backPage:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  bindGetUserInfo: function (e) {
    //登录远程服务器
    this.loginByWeixin(e.detail)
  },
  wxLogin: function(e) {//这个方法有时候获取用户的信息为   微信用户
    console.log("程序走到这里了！");
  
  },
  accountLogin: function() {
    wx.navigateTo({
      url: "/pages/auth/accountLogin/accountLogin"
    });
  }
})